home *** CD-ROM | disk | FTP | other *** search
- /* XThread.h
- *
- * This encapsulates thread support
- */
-
- /* YAAF - Yet another application framework
- * Copyright (C) 1997 William Edward Woody and In Phase Consulting
- *
- * This library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Library
- * General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or any
- * later version.
- *
- * This library is distributed in the hope that it will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Library General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Library General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * To contact the author, either e-mail me at
- * woody@alumni.caltech.edu, or write to us at
- *
- * William Edward Woody
- * In Phase Consulting
- * 1545 Ard Eevin Avenue
- * Glendale, CA 91202
- */
-
- #ifndef __XTHREAD_H__
- #define __XTHREAD_H__
-
- #include <XConfig.h>
-
- #if OPT_MACOS == 1
- #include <Threads.h>
- #endif
-
- #if defined(__MWERKS__)
- #if defined(macintosh)
- #pragma options align=power
- #endif
- #if defined(__INTEL__)
- #pragma pack(push,2)
- #endif
- #endif
-
- /************************************************************************/
- /* */
- /* Forwards */
- /* */
- /************************************************************************/
-
- class XGThread;
-
- /************************************************************************/
- /* */
- /* Thread Support */
- /* */
- /************************************************************************/
-
- typedef void (* XGThreadProcPtr)(XGThread *);
-
- /* XGThread
- *
- * This is the thread object; this knows how to handle, identify,
- * fold, and mutulate a thread object.
- *
- * Attach/Detach semantics provides me a way to manipulate a
- * thread, even after the thread dies. Normally, the XGThread class
- * pointer is owned by the thread--when the thread dies, the XGThread
- * class object is deleted. This sucks if you want to keep a pointer
- * to the thread object pass it's expiration.
- *
- * Thus, the Attach/Detach semantics allows other objects to
- * keep a pointer to the XGThread, and claim ownership past the
- * thread's termination.
- *
- * Note that when the thread is first created, it's owned by
- * the thread itself. That means if you want to create this thread
- * and keep a pointer to that thread, you need to explicitly attach
- * to the thread after it's creation: thus,
- *
- * XGThread *ptr = new XGThread(myProc,0,true); // create suspended
- * ptr->Attach(); // attach to it
- * ptr->Resume(); // and resume execution
- */
-
- class XGThread {
- public:
- XGThread(XGThreadProcPtr, long stack = 0,
- bool suspend = false);
- XGThread();
- virtual ~XGThread();
-
- /*
- * Attach/detach semantics--for other threads which want
- * to know information about this thread.
- */
-
- void Attach(void)
- {
- fAttach++;
- }
- void Detach(void)
- {
- if (--fAttach <= 0) delete this;
- }
- long GetAttach(void) const
- {
- return fAttach;
- }
-
- /*
- * Thread management routines
- */
-
- static XGThread *CurrentThread();
- static void YieldThread(bool f = false);
-
- /*
- * Scheduling routines
- */
-
- void Suspend(void);
- void Resume(void);
- bool IsSuspend(void); // Thread suspended?
-
- void Kill(void);
- bool IsAlive(void); // Valid thread?
-
- private:
- long fAttach;
- XGThreadProcPtr fProc;
-
- XGThread *fNext;
- static XGThread *gList;
-
- #if OPT_MACOS == 1
- ThreadID fThreadID;
- static pascal void *ThreadEntry(void *);
- static pascal void ThreadTerminate(ThreadID,void *);
- #endif
-
- #if OPT_WINOS == 1
- bool fThreadSuspend;
- HANDLE fThreadHandle;
- DWORD fThreadID;
-
- static DWORD WINAPI ThreadEntry(LPVOID);
- #endif
- };
-
- /* XGSemaphore
- *
- * This is a semaphore object. This allows me to create and
- * control access to a chunk of critical code
- */
-
- class XGSemaphore {
- public:
- XGSemaphore(unsigned long maxaccess = 1);
- virtual ~XGSemaphore();
-
- /*
- * Access routines
- */
-
- void EnterCritical(void);
- void LeaveCritical(void);
-
- private:
-
- #if OPT_MACOS == 1
- unsigned long fAccess;
- #endif
-
- #if OPT_WINOS == 1
- HANDLE fHandle;
- #endif
- };
-
- /* XGCritical
- *
- * Access object. This provides the enter/exit semantics in
- * a stack object for semaphore access; this provides a return-safe
- * and throw-safe way of providing enter/exit semaphore access
- */
-
- class XGCritical {
- public:
- XGCritical(XGSemaphore *x)
- {
- fSemaphore = x;
- x->EnterCritical();
- }
- ~XGCritical()
- {
- fSemaphore->LeaveCritical();
- }
-
- private:
- XGSemaphore *fSemaphore;
- };
-
- #if defined(__MWERKS__)
- #if defined(macintosh)
- #pragma options align=reset
- #endif
- #if defined(__INTEL__)
- #pragma pack(pop)
- #endif
- #endif
-
- #endif /* __XTHREAD_H__ */
-